package com.ljc;

/**
 * @author ljchen
 * @date 2022-08-08 11:58
 * @desc
 * Given an integer x, return true if x is palindrome integer.
 *
 * An integer is a palindrome when it reads the same backward as forward.
 *
 * For example, 121 is a palindrome while 123 is not.
 * Example 1:
 *
 * Input: x = 121
 * Output: true
 * Explanation: 121 reads as 121 from left to right and from right to left.
 * Example 2:
 *
 * Input: x = -121
 * Output: false
 * Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore, it is not a palindrome.
 * Example 3:
 *
 * Input: x = 10
 * Output: false
 * Explanation: Reads 01 from right to left. Therefore, it is not a palindrome.
 */
public class E9PalindromeNumber {

    public static void main(String[] args) {
        Integer a = 12345;
        Integer b = 123454321;
        Integer c = 12344321;
        Integer d = 123421;
        System.out.println("a: " + isPalindromeNumber(a));
        System.out.println("b: " + isPalindromeNumber(b));
        System.out.println("c: " + isPalindromeNumber(c));
        System.out.println("d: " + isPalindromeNumber(d));
    }


    public static boolean isPalindromeNumber(Integer x) {
        //将整数转化为string
        String y = String.valueOf(x);
        //解题思路：收尾各取一个字符，用于判断是否相同，若相同，则判断下一个对应收尾字符是否相同，依次类推！
        //比如先判断第一个字符，和最后一个字符是否相同，若相同，则继续判断第二个字符和倒数第二个字符是否相同，否则返回false!
        int start = 0;
        int end = y.length()-1;
        for (int i = start; i < y.length() && i <= end; i++) {
            char c = y.charAt(i);
            char d = y.charAt(end);
            if (!String.valueOf(c).equals(String.valueOf(d))) {
                return false;
            }else {
                end--;
            }
        }
        return true;
    }


}
